package com.chefu.juche.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.pojo.CfException;
import com.chefu.common.pojo.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.juche.mapper.EmployeeMapper;
import com.chefu.juche.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 员工Service
 */
@Service
@Transactional
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    //分页查询公司所有员工
    public PageResult<Employee> findByMerchantId(Integer page, Integer rows, Integer merchantId, String employeeName, String nickname, String phone) {
        IPage<Employee> iPage = new Page<>(page,rows);
        QueryWrapper<Employee> queryWrapper = Wrappers.query();
        if (merchantId!=null){
            queryWrapper.eq("merchantId",merchantId);
        }
        if (StringUtils.isNotEmpty(employeeName)){
            queryWrapper.like("employeeName",employeeName);
        }
        if (StringUtils.isNotEmpty(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        if (StringUtils.isNotEmpty(phone)){
            queryWrapper.eq("phone",phone);
        }
        List<Employee> employees = iPage.getRecords();
        if (CollectionUtils.isEmpty(employees)){
            throw new CfException(ExceptionEnum.EMPLOYEE_DETAIL_NOT_FOUND);
        }
        iPage = employeeMapper.selectPage(iPage,queryWrapper);
        PageResult<Employee> pageResult = new PageResult<>(iPage.getTotal(),iPage.getPages(),employees);
        return pageResult;
    }

    //根据员工id查询员工
    public Employee findByEmployeeId(Integer employeeId) {
        Employee employee = employeeMapper.selectById(employeeId);
        if (employee==null){
            throw new CfException(ExceptionEnum.EMPLOYEE_DETAIL_NOT_FOUND);
        }
        return employee;
    }

    //根据员工id锁定员工账号
    public void lockByEmployeeId(Integer employeeId) {
        try {
            Employee employee = this.findByEmployeeId(employeeId);
            employee.setStatus(!employee.getStatus());
            employeeMapper.updateById(employee);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    //根据员工id重置员工密码
    public void resetpswByemployeeId(Integer employeeId) {
        try {
            Employee employee = new Employee();
            employee.setEmployeeId(employeeId);
            employee.setPassword("123456");
            employeeMapper.updateById(employee);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    //创建公司管理员
    public void createAdminByMerchantId(Employee employee) {
        try {
            employeeMapper.insert(employee);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }
}
